package com.xiaoke.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoke.domain.ResponseResult;
import com.xiaoke.domain.entity.Comment;
import com.xiaoke.domain.vo.GetByArticleIdCommentVo;
import com.xiaoke.domain.vo.NewCommentListVo;
import com.xiaoke.mapper.CommentMapper;
import com.xiaoke.service.CommentService;
import com.xiaoke.utils.BeanCopyUtils;
import com.xiaoke.utils.CommentUtils;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

import static com.xiaoke.constants.SystemConstants.COMMENT_NEW_SIZE;
import static com.xiaoke.constants.SystemConstants.STATUS_NORMAL;

/**
 * 评论表(Comment)表服务实现类
 *
 * @author xiaoke
 * @since 2023-03-17 09:39:37
 */
@Service("commentService")
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    @Override
    public ResponseResult newCommentList() {
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper
                .eq(Comment::getStatus,STATUS_NORMAL)
                .orderByDesc(Comment::getCreateTime);

        Page<Comment> commentPage = new Page<>(1,COMMENT_NEW_SIZE);
        page(commentPage,queryWrapper);

        List<Comment> commentList = commentPage.getRecords();
        List<NewCommentListVo> newCommentListVos = BeanCopyUtils.copyBeanList(commentList, NewCommentListVo.class);

        return ResponseResult.okResult(newCommentListVos);
    }

    @Override
    public ResponseResult getByArticleIdComment(Long id) {
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper
                .eq(Comment::getStatus,STATUS_NORMAL)
                .eq(Comment::getArticleId,id)
                .orderByDesc(Comment::getCreateTime);

        List<Comment> commentList = list(queryWrapper);

        List<GetByArticleIdCommentVo> getByArticleIdCommentVos = BeanCopyUtils.copyBeanList(commentList, GetByArticleIdCommentVo.class);



        List<GetByArticleIdCommentVo> commentChildren = new ArrayList<>();

        List<Long> deleteCommentIds = new ArrayList<>();


        // TODO 性能非常差，优化优化优化
        for (GetByArticleIdCommentVo comment : getByArticleIdCommentVos) {
            // 判断是否顶级
            if(comment.getPid() == 0) {
                // 查询子元素
                for (GetByArticleIdCommentVo commentVo : getByArticleIdCommentVos) {
                    if(Objects.equals(commentVo.getPid(), comment.getId())) {
                        commentChildren.add(commentVo);
                        deleteCommentIds.add(commentVo.getId());
                    }
                }
                // 添加子元素
                for (GetByArticleIdCommentVo commentVo : getByArticleIdCommentVos) {
                    if(Objects.equals(commentVo.getPid(), comment.getId())) {
                        comment.setChildren(commentChildren);
                    }
                }

            }
        }

        // 删除重复的
        for (Long delId : deleteCommentIds) {
            commentList.removeIf(next -> Objects.equals(next.getId(), delId));
        }


        return ResponseResult.okResult(getByArticleIdCommentVos);
    }





}

